home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // CtlDemo4.cpp
- //
- //***********************************************************************
-
- #include <afxwin.h>
- #include "CtlDemo4.h"
-
- #define IDC_RED 100
- #define IDC_GREEN 101
- #define IDC_BLUE 102
-
- CMyApp myApp;
-
- /////////////////////////////////////////////////////////////////////////
- // CMyApp member functions
-
- BOOL CMyApp::InitInstance ()
- {
- m_pMainWnd = new CMainWindow;
- m_pMainWnd->ShowWindow (m_nCmdShow);
- m_pMainWnd->UpdateWindow ();
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CMainWindow message map and member functions
-
- BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
- ON_WM_CREATE ()
- ON_WM_CTLCOLOR ()
- ON_BN_CLICKED (IDC_RED, OnButtonClicked)
- ON_BN_CLICKED (IDC_GREEN, OnButtonClicked)
- ON_BN_CLICKED (IDC_BLUE, OnButtonClicked)
- END_MESSAGE_MAP ()
-
- CMainWindow::CMainWindow ()
- {
- CString strWndClass = AfxRegisterWndClass (
- 0,
- myApp.LoadStandardCursor (IDC_ARROW),
- (HBRUSH) (COLOR_3DFACE + 1),
- myApp.LoadStandardIcon (IDI_APPLICATION)
- );
-
- Create (strWndClass, "CtlDemo4");
- }
-
- int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
- {
- if (CFrameWnd::OnCreate (lpcs) == -1)
- return -1;
-
- CClientDC dc (this);
- int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * 8) / 72);
-
- m_font.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
- DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
- DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
-
- CFont* pOldFont = dc.SelectObject (&m_font);
- TEXTMETRIC tm;
- dc.GetTextMetrics (&tm);
- m_cxChar = tm.tmAveCharWidth;
- m_cyChar = tm.tmHeight + tm.tmExternalLeading;
- dc.SelectObject (pOldFont);
-
- m_ctlGroupBox1.Create ("Sample text", WS_CHILD | WS_VISIBLE |
- BS_GROUPBOX, CRect (m_cxChar * 2, m_cyChar, m_cxChar * 62,
- m_cyChar * 8), this, UINT (-1));
-
- m_ctlText.Create ("Click a button to change my color",
- WS_CHILD | WS_VISIBLE | SS_CENTER, CRect (m_cxChar * 4,
- m_cyChar * 4, m_cxChar * 60, m_cyChar * 6), this);
-
- m_ctlGroupBox2.Create ("Color", WS_CHILD | WS_VISIBLE |
- BS_GROUPBOX, CRect (m_cxChar * 64, m_cyChar, m_cxChar * 80,
- m_cyChar * 8), this, UINT (-1));
-
- m_ctlRadioButtonRed.Create ("Red", WS_CHILD | WS_VISIBLE | WS_GROUP |
- BS_AUTORADIOBUTTON, CRect (m_cxChar * 66, m_cyChar * 3,
- m_cxChar * 78, m_cyChar * 4), this, IDC_RED);
-
- m_ctlRadioButtonGreen.Create ("Green", WS_CHILD | WS_VISIBLE |
- BS_AUTORADIOBUTTON, CRect (m_cxChar * 66, (m_cyChar * 9) / 2,
- m_cxChar * 78, (m_cyChar * 11) / 2), this, IDC_GREEN);
-
- m_ctlRadioButtonBlue.Create ("Blue", WS_CHILD | WS_VISIBLE |
- BS_AUTORADIOBUTTON, CRect (m_cxChar * 66, m_cyChar * 6,
- m_cxChar * 78, m_cyChar * 7), this, IDC_BLUE);
-
- m_ctlRadioButtonRed.SetCheck (1);
-
- m_ctlGroupBox1.SetFont (&m_font, FALSE);
- m_ctlGroupBox2.SetFont (&m_font, FALSE);
- m_ctlRadioButtonRed.SetFont (&m_font, FALSE);
- m_ctlRadioButtonGreen.SetFont (&m_font, FALSE);
- m_ctlRadioButtonBlue.SetFont (&m_font, FALSE);
- return 0;
- }
-
- HBRUSH CMainWindow::OnCtlColor (CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- if (pWnd->m_hWnd == m_ctlText.m_hWnd) {
- COLORREF crColor;
- if (m_ctlRadioButtonRed.GetCheck ())
- crColor = RGB (255, 0, 0);
- else if (m_ctlRadioButtonGreen.GetCheck ())
- crColor = RGB (0, 255, 0);
- else
- crColor = RGB (0, 0, 255);
-
- pDC->SetTextColor (crColor);
- pDC->SetBkColor (::GetSysColor (COLOR_3DFACE));
- return ::GetSysColorBrush (COLOR_3DFACE);
- }
- return CFrameWnd::OnCtlColor (pDC, pWnd, nCtlColor);
- }
-
- void CMainWindow::OnButtonClicked ()
- {
- m_ctlText.Invalidate ();
- }
-